package com.mercadoutils.controller;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.mercadolibre.sdk.Meli;
import com.mercadolibre.sdk.MeliException;
import com.mercadoutils.listeners.SesionListener;
import com.ning.http.client.FluentStringsMap;
import com.ning.http.client.Response;

@Controller
public class TestUserController {

	    protected final Logger logger = Logger.getLogger(getClass());
	    
	    @RequestMapping("/testUser.html")
	    public String handleRequest(Model model) throws MeliException, IOException, ServletException, SQLException {
	 
	        logger.debug("Test user created");
	        HttpSession session = SesionListener.getSession();
	        Meli m = (Meli) session.getAttribute("meli");
	        
	        FluentStringsMap params = new FluentStringsMap();
	        params.add("access_token", m.getAccessToken());
	        Response r = m.post("/users/test_user", params,"{\"site_id\":\"MLA\"}");
	        System.out.println(r.getResponseBody().toString());
	        
	        return "home";
	    }
}

